import axios from 'axios'
import Cookies from 'js-cookie'



axios.defaults.baseURL='http://127.0.0.1:8000/';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
if (Cookies.get('token')) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
  axios.defaults.headers.common['Authorization'] = `JWT ${Cookies.get('token')}`;
}



//点赞文章
export const articlePraise= (parms) => {
  return new Promise((resolve, reject) => {
    axios.post('/api/userPraise/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//收藏文章
export const articleCollection= (parms) => {
  return new Promise((resolve, reject) => {
    axios.post('/api/userFav/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//关注用户
export const userFollow= (parms) => {
  return new Promise((resolve, reject) => {
    axios.post('/api/userFollow/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//拉取点赞文章
export const getArticlePraise= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/userPraise/' + parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//拉取收藏文章列表
export const getArticleCollection= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/userFav/' + parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//拉取关注的用户列表
export const getUserFollow= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/userFollow/' + parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//判断是否已经点赞
export const isPraise= (id) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/userPraise/'+ id + '/').then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//判断是否已经收藏
export const isCollection= (id) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/userFav/'+ id + '/').then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//判断是否已经关注
export const isFollow= (id) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/userFollow/'+ id + '/').then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//取消点赞
export const unPraise= (id) => {
  return new Promise((resolve, reject) => {
    axios.delete('/api/userPraise/'+ id + '/').then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//取消收藏
export const unCollection= (id) => {
  return new Promise((resolve, reject) => {
    axios.delete('/api/userFav/'+ id + '/').then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//取消关注
export const unFollow= (id) => {
  return new Promise((resolve, reject) => {
    axios.delete('/api/userFollow/'+ id + '/').then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//消息中心
export const getMessage= (params) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/message/'+ params).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};
